package com.feonix.common.util.jdbc;

import com.feonix.common.annotation.ColumnType;
import com.feonix.common.basedao.EntityProperty;
import com.feonix.common.util.StringUtil;

public class MySQLDialect extends AbsDialect implements Dialect {

	protected static final String SQL_END_DELIMITER = ";";

	public String getLimitString(String sql, int offset, int limit) {
		sql = trim(sql);
		StringBuffer stringBuffer = new StringBuffer(sql.length() + 20);
		stringBuffer.append(sql);
		if (offset > 0)
			stringBuffer.append(" limit ").append(offset).append(',').append(limit).append(";");
		else {
			stringBuffer.append(" limit ").append(limit).append(";");
		}
		return stringBuffer.toString();
	}

	public boolean supportsLimit() {
		return true;
	}

	private String trim(String sql) {
		sql = sql.trim();
		if (sql.endsWith(";")) {
			sql = sql.substring(0, sql.length() - 1 - ";".length());
		}
		return sql;
	}

	public String getDateColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" date");
		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getDateTimeColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer sql = new StringBuffer();
		sql.append(entityProperty.getColumnName()).append(" datetime");
		setConstraint(entityProperty, sql);
		str = sql.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { sql });
		return str;
	}

	public String getIntColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" int(").append(entityProperty.getLength())
				.append(")");
		if (StringUtil.isNotEmpty(entityProperty.getDefaultValue())) {
			stringBuffer.append(" DEFAULT " + entityProperty.getDefaultValue());
		}
		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getDoubleColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" decimal(").append(entityProperty.getLength())
				.append(",").append(entityProperty.getDecimalLenght()).append(")");
		if (StringUtil.isNotEmpty(entityProperty.getDefaultValue())) {
			stringBuffer.append(" DEFAULT " + entityProperty.getDefaultValue());
		}
		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getFloatColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" decimal(").append(entityProperty.getLength())
				.append(",").append(entityProperty.getDecimalLenght()).append(")");
		if (StringUtil.isNotEmpty(entityProperty.getDefaultValue())) {
			stringBuffer.append(" DEFAULT " + entityProperty.getDefaultValue());
		}
		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getVarcharColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" varchar(").append(entityProperty.getLength())
				.append(")");
		if (StringUtil.isNotEmpty(entityProperty.getDefaultValue())) {
			stringBuffer.append(" DEFAULT '" + entityProperty.getDefaultValue() + "'");
		}
		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getCharColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" char(").append(entityProperty.getLength())
				.append(")");
		if (StringUtil.isNotEmpty(entityProperty.getDefaultValue())) {
			stringBuffer.append(" DEFAULT '" + entityProperty.getDefaultValue() + "'");
		}
		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getTextColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" mediumtext");
		if (StringUtil.isNotEmpty(entityProperty.getDefaultValue())) {
			stringBuffer.append(" DEFAULT '" + entityProperty.getDefaultValue() + "'");
		}
		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getBlobColumn(EntityProperty entityProperty) {
		String str = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(entityProperty.getColumnName()).append(" longblob");

		setConstraint(entityProperty, stringBuffer);
		str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getGeneratorColumn(EntityProperty entityProperty) {
		StringBuffer stringBuffer = new StringBuffer();
		switch (entityProperty.getGeneratorType().ordinal()) {
		case 0:
			stringBuffer.append(" NOT NULL AUTO_INCREMENT");
		}

		if (entityProperty.isPk()) {
			stringBuffer.append(",PRIMARY KEY (").append(entityProperty.getColumnName()).append(")");
		}
		String str = stringBuffer.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { stringBuffer });
		return str;
	}

	public String getCharset() {
		return "ENGINE=InnoDB DEFAULT CHARSET=utf8";
	}

	public String addColumn(String table, String column, ColumnType columnType, String length, String defaultVal) {
		StringBuffer sql = new StringBuffer();
		sql.append("ALTER TABLE ").append(table).append(" ADD COLUMN ").append(column).append(" ")
				.append(getColumnTypeString(columnType));
		if (StringUtil.isNotEmpty(length)) {
			sql.append("(").append(length).append(")");
		}
		if (StringUtil.isNotEmpty(defaultVal)) {
			sql.append(" DEFAULT ").append(defaultVal);
		}
		String str = sql.toString();
		StringUtil.cleanStringBuffer(new StringBuffer[] { sql });
		return str;
	}

	public String updateColumn(String tableName, String oldColumn, EntityProperty entityProperty) {
		return null;
	}

	private String getColumnTypeString(ColumnType columnType) {
		String str = null;
		switch (columnType.ordinal()) {
		case 0:
			str = "date";
			break;
		case 1:
			str = "datetime";
			break;
		case 2:
			str = "int";
			break;
		case 3:
			str = "decimal";
			break;
		case 4:
			str = "decimal";
			break;
		case 5:
			str = "varchar";
		case 6:
			str = "CHAR";
			break;
		case 7:
			str = "mediumtext";
			break;
		case 8:
			str = "longblob";
			break;
		case 9:
			str = "mediumtext";
			break;
		}

		return str;
	}

}
